﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Table = FCNS.Data.Table;
using System.Collections;
using DoubleH.Utility;
using DoubleH.Utility.Configuration;

namespace ReportS.UC
{
    /// <summary>
    /// UCSearchOrder.xaml 的交互逻辑
    /// </summary>
    public partial class UCSearchOrder : UserControl
    {
        public UCSearchOrder()
        {
            InitializeComponent();
        }

        DataTableText tableText;
        IEnumerable data = null;
        DateTime? start = null;
        DateTime? end = null;
        int[] enableValue = null;
        int[] flagValue = null;
        int[] StoreSValue = null;
        int[] GroupSValue = null;

        bool showStoreS =true;

        public void Init(DataTableText obj)
        {
            tableText = obj;

            InitVar();
            InitEvent();
            InitControls();
        }

        private void InitControls()
        {
            comboBoxDate.SelectedIndex = 3;

            checkComboBoxGroupS.DisplayMemberPath = "Name";
            checkComboBoxGroupS.ValueMemberPath = "Id";

            if (showStoreS)
            {
                checkComboBoxStoreS.DisplayMemberPath = "Name";
                checkComboBoxStoreS.ValueMemberPath = "Id";
                checkComboBoxStoreS.ItemsSource = Table.StoreS.GetList(Table.StoreS.EnumEnable.启用);
  
            }
            //else
            //{
            //    labelStore.Visibility = Visibility.Hidden;
            //    checkComboBoxStoreS.Visibility = Visibility.Hidden;
            //}
        }

        private void InitEvent()
        {
            comboBoxDate.SelectionChanged += (ss, ee) => { SetDateTime(); };
        }

        private void SetDateTime()
        {
            string start = "19840924" + DoubleHConfig.UserConfig.WorkStart;
            string end = "19890615" + DoubleHConfig.UserConfig.WorkEnd;
            switch (comboBoxDate.SelectedIndex)
            {
                case 0:
                    start = DateTime.Now.ToString("yyyyMMdd") + DoubleHConfig.UserConfig.WorkStart;
                    end = DateTime.Now.ToString("yyyyMMdd") +DoubleHConfig.UserConfig.WorkEnd;
                    break;
                case 1:
                    start = DateTime.Now.AddDays((DateTime.Now.DayOfWeek == DayOfWeek.Sunday ? -6 : 1 - (int)DateTime.Now.DayOfWeek)).ToString("yyyyMMdd") + DoubleHConfig.UserConfig.WorkStart;
                    end = DateTime.Now.AddDays((DateTime.Now.DayOfWeek == DayOfWeek.Sunday ? 0 : 7 - (int)DateTime.Now.DayOfWeek)).ToString("yyyyMMdd") + DoubleHConfig.UserConfig.WorkEnd;
                    break;
                case 2:
                    start = DateTime.Now.AddDays((DateTime.Now.DayOfWeek == DayOfWeek.Sunday ? -13 : -6 - (int)DateTime.Now.DayOfWeek)).ToString("yyyyMMdd") + DoubleHConfig.UserConfig.WorkStart;
                    end = DateTime.Now.AddDays((DateTime.Now.DayOfWeek == DayOfWeek.Sunday ? -7 : 0 - (int)DateTime.Now.DayOfWeek)).ToString("yyyyMMdd") + DoubleHConfig.UserConfig.WorkEnd;
                    break;
                case 3:
                    start = DateTime.Now.AddDays(1 - DateTime.Now.Day).ToString("yyyyMMdd") + DoubleHConfig.UserConfig.WorkStart;
                    end = DateTime.Now.AddDays(DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month) - DateTime.Now.Day).ToString("yyyyMMdd") + DoubleHConfig.UserConfig.WorkEnd;
                    break;
                case 4:
                    int m = DateTime.Now.Month;
                    start = DateTime.Now.AddMonths(-1).ToString("yyyyMM") + "01" + DoubleHConfig.UserConfig.WorkStart;
                    end = DateTime.Now.AddMonths(-1).ToString("yyyyMM") + DateTime.DaysInMonth(DateTime.Now.AddMonths(-1).Year, DateTime.Now.AddMonths(-1).Month) + DoubleHConfig.UserConfig.WorkEnd;
                    break;
                case 5:
                    start = DateTime.Now.Year.ToString() + "0101" + DoubleHConfig.UserConfig.WorkStart;
                    end = DateTime.Now.Year.ToString() + "12" + DateTime.DaysInMonth(DateTime.Now.Year, 12).ToString() + DoubleHConfig.UserConfig.WorkEnd;
                    break;
            }

            dateTimePickerStart.Value = DateTime.ParseExact(start, FCNS.Data.DbDefine.dateTimeFormat, null);
            dateTimePickerEnd.Value = DateTime.ParseExact(end, FCNS.Data.DbDefine.dateTimeFormat, null);
        }

        private bool ReadSql()
        {
            bool read = false;
            if (start != dateTimePickerStart.Value)
                read = true;
             if (end != dateTimePickerEnd.Value)
                read = true;
             if (EnableValueChanged())
                read = true;
             if (FlagValueChanged())
                read = true;
             if (StoreSValueChanged())
                 read = true;
             if (GroupSValueChanged())
                 read = true;

            if (read)
            {
                start = dateTimePickerStart.Value;
                end = dateTimePickerEnd.Value;
            }

            return read;
        }


        private void InitVar()
        {
            switch (tableText)
            {
                #region 仓库
                case DataTableText.商品出入库明细表:
                    checkComboBoxGroupS.ItemsSource = Table.GroupS.GetList(Table.GroupS.EnumFlag.商品分类,Table.GroupS.EnumEnable.启用);
                    break;
                #endregion

                #region 采购
                case DataTableText.采购订单:
                case DataTableText.采购退货单:
                case DataTableText.赠品入库单:
                    checkComboBoxEnable.ItemsSource = Enum.GetNames(typeof(Table.PurchaseOrderS.EnumEnable));
                    break;
                case DataTableText.采购统计表:
                    checkComboBoxEnable.ItemsSource = Enum.GetNames(typeof(Table.PurchaseOrderS.EnumEnable));
                    checkComboBoxFlag.ItemsSource = Enum.GetNames(typeof(Table.PurchaseOrderS.EnumFlag));
                    break;
                #endregion

                #region 销售
                case DataTableText.销售订单:
                case DataTableText.销售退货单:
                case DataTableText.赠品出库单:
                    checkComboBoxEnable.ItemsSource = Enum.GetNames(typeof(Table.SalesOrderS.EnumEnable));
                    break;
                case DataTableText.销售统计表:
                    checkComboBoxEnable.ItemsSource = Enum.GetNames(typeof(Table.SalesOrderS.EnumEnable));
                    checkComboBoxFlag.ItemsSource = Enum.GetNames(typeof(Table.SalesOrderS.EnumFlag));
                    break;
                #endregion

                #region 售后
                //case DataTableText.故障库:
                //    showStoreS = false;
                //    labelEnable.Content = "维修对象:";
                //    checkComboBoxEnable.ItemsSource = Table.UniqueS.GetList(Table.UniqueS.EnumFlag.维修对象);
                //    checkComboBoxEnable.DisplayMemberPath = "Name";
                //    checkComboBoxEnable.ValueMemberPath = "Id";

                //    labelFlag.Content = "维修类型:";
                //    checkComboBoxFlag.ItemsSource = Table.UniqueS.GetList(Table.UniqueS.EnumFlag.维修类型);
                //    checkComboBoxFlag.DisplayMemberPath = "Name";
                //    checkComboBoxFlag.ValueMemberPath = "Id";
                //    break;
                case DataTableText.售后统计表:
                    showStoreS = false;
                    break;
                #endregion

                #region 其它
                case DataTableText.可采购商品:
                case DataTableText.可销售商品:
                case DataTableText.仓库商品:
                       checkComboBoxEnable.ItemsSource = Enum.GetNames(typeof(Table.ProductS.EnumEnable));
                       checkComboBoxGroupS.ItemsSource = Table.GroupS.GetList(Table.GroupS.EnumFlag.商品分类,Table.GroupS.EnumEnable.启用);
                        break;

                case DataTableText.供应商:
                    showStoreS = false;
                    checkComboBoxEnable.ItemsSource = Enum.GetNames(typeof(Table.CorS.EnumEnable));
                    checkComboBoxGroupS.ItemsSource = Table.GroupS.GetList(Table.GroupS.EnumFlag.客商分类,Table.GroupS.EnumEnable.启用);
                    break;
                case DataTableText.客户:
                    showStoreS = false;
                    checkComboBoxEnable.ItemsSource = Enum.GetNames(typeof(Table.CorS.EnumEnable));
                    checkComboBoxGroupS.ItemsSource = Table.GroupS.GetList(Table.GroupS.EnumFlag.客商分类,Table.GroupS.EnumEnable.启用);
                    break;
                #endregion
            }
        }

        private bool EnableValueChanged()
        {
            int[] it = null;

            #region
            if (!string.IsNullOrEmpty(checkComboBoxEnable.Text))
            {
                string[] tt = checkComboBoxEnable.SelectedValue.Split(',');

                it = new int[tt.Length];
                for (int i = 0; i < tt.Length; i++)
                {
                    switch (tableText)
                    {
                        case DataTableText.故障库:
                             it[i] = Convert.ToInt32( tt[i]);
                            break;

                        case DataTableText.采购询价单:
                        case DataTableText.采购订单:
                        case DataTableText.采购退货单:
                        case DataTableText.赠品入库单:
                        case DataTableText.采购统计表:
                            it[i] = Convert.ToInt32(Enum.Parse(typeof(Table.PurchaseOrderS.EnumEnable), tt[i]));
                            break;

                        case DataTableText.销售询价单:
                        case DataTableText.销售订单:
                        case DataTableText.销售退货单:
                        case DataTableText.赠品出库单:
                        case DataTableText.销售统计表:
                            it[i] = Convert.ToInt32(Enum.Parse(typeof(Table.SalesOrderS.EnumEnable), tt[i]));
                            break;

                        case DataTableText.供应商:
                        case DataTableText.客户:
                        case DataTableText.会员资料:
                            it[i] = Convert.ToInt32(Enum.Parse(typeof(Table.CorS.EnumEnable), tt[i]));
                            break;

                        case DataTableText.可采购商品:
                        case DataTableText.可销售商品:
                        case DataTableText.仓库商品:
                             it[i] = Convert.ToInt32(Enum.Parse(typeof(Table.ProductS.EnumEnable), tt[i]));
                            break;
                    }
                }
            }
            #endregion

            if (it != enableValue)
            {
                enableValue = it;
                return true;
            }
            else
                return false;
        }

        private bool FlagValueChanged()
        {
            int[] it = null;
            #region
            if (!string.IsNullOrEmpty(checkComboBoxFlag.Text))
            {
                string[] tt = checkComboBoxFlag.SelectedValue.Split(',');
                it = new int[tt.Length];
                for (int i = 0; i < tt.Length; i++)
                {
                    switch (tableText)
                    {
                        case DataTableText.故障库:
                            it[i] = Convert.ToInt32(tt[i]);
                            break;

                        case DataTableText.采购统计表:
                            it[i] = Convert.ToInt32(Enum.Parse(typeof(Table.PurchaseOrderS.EnumFlag), tt[i]));
                            break;

                        case DataTableText.销售统计表:
                            it[i] = Convert.ToInt32(Enum.Parse(typeof(Table.SalesOrderS.EnumFlag), tt[i]));
                            break;
                    }
                }
            }
            #endregion

            if (it != flagValue)
            {
                flagValue = it;
                return true;
            }
            else
                return false;
        }

        private bool StoreSValueChanged()
        {
            int[] it = null;
            if (!string.IsNullOrEmpty(checkComboBoxStoreS.Text))
            {
                string[] tt = checkComboBoxStoreS.SelectedValue.Split(',');
                it = new int[tt.Length];
                for (int i = 0; i < tt.Length; i++)
                    it[i] = Convert.ToInt32(tt[i]);
            }

            if (it != StoreSValue)
            {
                StoreSValue = it;
                return true;
            }
            else
                return false;
        }

        private bool GroupSValueChanged()
        {
            int[] it = null;
            if (!string.IsNullOrEmpty(checkComboBoxGroupS.Text))
            {
                string[] tt = checkComboBoxGroupS.SelectedValue.Split(',');
                it = new int[tt.Length];
                for (int i = 0; i < tt.Length; i++)
                    it[i] = Convert.ToInt32(tt[i]);
            }

            if (it != GroupSValue)
            {
                GroupSValue = it;
                return true;
            }
            else
                return false;
        }


        /// <summary>
        /// 从数据库读取数据
        /// </summary>
        /// <returns></returns>
        public IEnumerable GetSearchData()
        {
            if (!ReadSql())
                return data;

            StringBuilder sb = new StringBuilder();
            switch (tableText)
            {
                #region 售后
                case DataTableText.售后统计表:
                    data = Table.AfterSaleServiceS.GetList(dateTimePickerStart.Value, dateTimePickerEnd.Value, null, Table.AfterSaleServiceS.EnumEnable.维护完工, Table.AfterSaleServiceS.EnumEnable.回访反馈, Table.AfterSaleServiceS.EnumEnable.员工评价, Table.AfterSaleServiceS.EnumEnable.作废);
                    break;

                case DataTableText.故障库:
                sb.Append("select GuZhangMiaoShu,JieJueFangFa,CorSId from AfterSaleServiceS where ");
                    sb.Append("(cast(OrderDateTime as varchar)>='" +start.Value.ToString(FCNS.Data.DbDefine.dateTimeFormat) + "'");
                    sb.Append(" and cast(OrderDateTime as varchar)<='" + end.Value.ToString(FCNS.Data.DbDefine.dateTimeFormat) + "')");

                    if (enableValue != null && enableValue.Length != 0)
                    {
                        sb.Append(" and (");
                        foreach (Int64 i in enableValue)
                            sb.Append("WeiXiuDuiXiang=" + i + " or ");

                        sb.Remove(sb.Length - 3, 3);
                        sb.Append(")");
                    }
                    if (flagValue != null && flagValue.Length != 0)
                    {
                        sb.Append(" and (" );
                        foreach (Int64 i in flagValue)
                            sb.Append("WeiXiuDuiXiang=" + i + " or ");

                        sb.Remove(sb.Length - 3, 3);
                        sb.Append(")");
                    }
                    data = FCNS.Data.SQLdata.GetDataRows(sb.ToString());
                    break;
                #endregion

                #region 仓库
                case DataTableText.商品出入库明细表:
                     sb.Append("select * from ProductSIO where Enable="+(int)Table.ProductSIO.EnumEnable.入账);
                    sb.Append(" and (cast(OrderDateTime as varchar)>='" +start.Value.ToString(FCNS.Data.DbDefine.dateTimeFormat) + "'");
                    sb.Append(" and cast(OrderDateTime as varchar)<='" + end.Value.ToString(FCNS.Data.DbDefine.dateTimeFormat) + "')");

                    if (StoreSValue!=null && StoreSValue.Length>0)
                    {
                        sb.Append(" and (");
                        foreach (Int64 i in StoreSValue)
                            sb.Append("StoreSId=" + i + " or ");

                        sb.Remove(sb.Length - 3, 3);
                        sb.Append(")");
                    }
                    if (GroupSValue != null && GroupSValue.Length > 0)
                    {
                        sb.Append(" and ProductSId in ( select Id from ProductS where (");
                        foreach (int i in GroupSValue)
                            sb.Append("GroupSId = " + i + " or ");

                        sb.Remove(sb.Length - 3, 3);
                        sb.Append("))");
                    }

                   data = FCNS.Data.SQLdata.GetDataRows(sb.ToString());
                    break;
                #endregion

                #region 采购
                case DataTableText.采购订单:
                case DataTableText.采购退货单:
                case DataTableText.赠品入库单:
                    data = Table.PurchaseOrderS.GetList(dateTimePickerStart.Value, dateTimePickerEnd.Value,
                        new int[]{(int)Enum.Parse(typeof(Table.PurchaseOrderS.EnumFlag),tableText.ToString())}, enableValue,StoreSValue);
                    break;

                case DataTableText.采购统计表:
                    data = Table.PurchaseOrderS.GetList(dateTimePickerStart.Value, dateTimePickerEnd.Value, flagValue, enableValue, StoreSValue);
                    break;
                #endregion

                #region 销售
                case DataTableText.销售订单:
                case DataTableText.销售退货单:
                case DataTableText.赠品出库单:
                    data = Table.SalesOrderS.GetList(dateTimePickerStart.Value, dateTimePickerEnd.Value,
                        new int[] { (int)Enum.Parse(typeof(Table.SalesOrderS.EnumFlag), tableText.ToString()) }, enableValue, StoreSValue);
                    break;

                case DataTableText.销售统计表:
                    data = Table.SalesOrderS.GetList(dateTimePickerStart.Value, dateTimePickerEnd.Value, flagValue, enableValue);
                    break;
                #endregion

                #region 其它
                case DataTableText.可采购商品:
                    data = Table.ProductS.GetList(0, 0, GroupSValue, enableValue, new int[] { (int)Table.ProductS.EnumFlag.可采购, (int)Table.ProductS.EnumFlag.可销售和可采购 });
                    break;
                case DataTableText.可销售商品:
                    data = Table.ProductS.GetList(0, 0, GroupSValue, enableValue, new int[] { (int)Table.ProductS.EnumFlag.可销售, (int)Table.ProductS.EnumFlag.可销售和可采购 });
                    break;
                case DataTableText.仓库商品:
                    data = Table.ProductS.GetList(0, 0, GroupSValue, enableValue, null);
                    break;

                case DataTableText.供应商:
                    data = Table.CorS.GetList(0, 0, GroupSValue, enableValue, new int[] { (int)Table.CorS.EnumFlag.供应商, (int)Table.CorS.EnumFlag.客户和供应商 });
                    break;
                case DataTableText.客户:
                    data = Table.CorS.GetList(0, 0, GroupSValue, enableValue, new int[] { (int)Table.CorS.EnumFlag.客户, (int)Table.CorS.EnumFlag.客户和供应商 });
                    break;
                #endregion

                default:
                    MessageWindow.Show("功能不支持");
                    break;
            }

            return data;
        }
    }
}